package com.mapperdb.db.sql;

import org.apache.ibatis.session.SqlSession;

import com.mapperdb.core.Assert;
import com.mapperdb.db.Config;
import com.mapperdb.db.DbKit;

public class SqlPro {
	private final Config config;
	private final String sql;

	// --------------------------------------------------------
	// c
	// --------------------------------------------------------
	public SqlPro(Config config, String sql) {
		Assert.hasLength(sql, "sql can not be blank");
		Assert.notNull(config, "Config can not be null");
		this.config = config;
		this.sql = sql;
	}

	// --------------------------------------------------------
	// use SqlSession
	// --------------------------------------------------------
	public static SqlPro use(SqlSession sqlSession, String sql) {
		Config config = DbKit.getConfig(sqlSession.getConfiguration());
		Assert.hasLength(sql, "sql can not be blank");
		Assert.notNull(config, "The SqlSession [" + sqlSession + "] not initialize ActiveRecord");
		return use(Config.newConfig(config, sqlSession), sql);
	}

	// --------------------------------------------------------
	// use Config
	// --------------------------------------------------------
	public static SqlPro use(String sql) {
		Assert.hasLength(sql, "sql can not be blank");
		return use(DbKit.getConfig().getName(), sql);
	}

	public static SqlPro use(String configName, String sql) {
		Assert.hasLength(configName, "configName can not be blank");
		Assert.hasLength(sql, "tableName can not be blank");
		Config config = DbKit.getConfig(configName);
		Assert.notNull(config, "Config not found by configName: " + configName);
		return use(config, sql);
	}

	public static SqlPro use(Config config, String sql) {
		Assert.notNull(config, "Config can not be null");
		Assert.hasLength(sql, "sql can not be blank");
		return new SqlPro(config, sql);
	}

	
}
